const Router = VueRouter;
import HelloWorld from '@/components/HelloWorld'
import store from "../store";
// import {message} from 'ant-design-vue'
Vue.use(Router)

const router = new Router({
    mode: 'history',
    base: '/vueapp/',
    routes: [
      {
        path: '/HelloWorld',
        name: 'HelloWorld',
        component: HelloWorld
      },
      {
        path: '/',
        name: 'Home',
        component: () => import('@/pages/Home'),
        meta: {
          title: '主页'
        },
        children: [
          {
            path: '/',
            name: 'HomePage',
            component: () => import('@/pages/main/HomePage'),
            meta: {
              title: '主页'
            }
          },
          {
            path: '/show_page',
            name: 'ShowPage',
            component: () => import('@/pages/main/ShowPage'),
            meta: {
              title: '展示列表'
            }
          },
          {
            path: '/discuss_page',
            name: 'DiscussPage',
            component: () => import('@/pages/main/DiscussPage'),
            meta: {
              title: '讨论组',
            }
          },
          {
            path: '/test_factory',
            name: 'TestFactory',
            component: () => import('@/pages/main/TestFactory'),
            meta: {
              loginRequire: true,
              title: '测试工厂'
            }
          },
          {
            path: '/profile',
            name: 'Profile',
            component: () => import('@/pages/profile/Profile'),
            meta: {
              loginRequire: true,
              title: '个人资料'
            }
          },
          {
            path: '/profile_edit',
            name: 'ProfileEdit',
            component: () => import('@/pages/profile/ProfileEdit'),
            meta: {
              loginRequire: true,
              title: '个人资料编辑'
            }
          },
          {
            path: '/article/:user_name/:article_id',
            name: 'Article',
            component: () => import('@/pages/article/Article'),
            meta: {
              title: '文章详情'
            }
          },
          {
            path: '/article/add',
            name: 'ArticleAdd',
            component: () => import('@/pages/article/ArticleAdd'),
            meta: {
              loginRequire: true,
              title: '文章发布'
            }
          }
        ]
      },
      {
        path: '/login',
        name: 'Login',
        component: () => import('@/pages/Login'),
        meta: {
          title: '登录'
        }
      },
      {
        path: '/register',
        name: 'Register',
        component: () => import('@/pages/Register'),
        meta: {
          title: '注册'
        }
      }
    ]
  })

router.beforeEach((to, from, next) => {
  // 标题切换
  document.querySelector('title').innerText = to.meta.title;
  // 登录验证
  let getFlag = localStorage.getItem("Flag");
  if(getFlag === "isLogin"){
    store.state.isLogin = true;
    next();
    if (!to.meta.loginRequire) {
      // iView.Message.error('请先退出登录');
      // next({
      //   path: '/home_page'
      // })
    }
  }else{
    // if(to.meta.loginRequire){
    if(false){
      // next({
      //   path: '/login',
      // });
      router.push({name: 'Login'});
      let message = antd.message;
      message.info('请先登录')
    }else{
      next()
    }
  }
});
export default router
